package com.github.pig.admin.controller.park;

import com.github.pig.admin.model.entity.SysDept;
import com.github.pig.admin.model.park.UserAppDto;
import com.github.pig.admin.service.park.IEmployeeInfoService;
import com.github.pig.admin.service.sys.SysDeptService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 职工信息
 *
 * @author sxf
 * @date 2018-12-25 10:23:32
 */
@Controller
@RequestMapping("/employee")
@Slf4j
public class EmployeeController extends BaseController {

    @Autowired
    private IEmployeeInfoService employeeService;
    @Autowired
    private SysDeptService deptService;


    /**
     * 获取当前登陆人信息
     *
     * @return
     */
    @GetMapping("/bindCompanyInfo")
    @ResponseBody
    public AjaxJson bindCompanyInfo() {
        AjaxJson json = new AjaxJson();
        try {
            json.setSuccess(true);
            //当前登陆名
            String loginName = getUserName();
            if (StringUtils.isBlank(loginName)) {
                json.setSuccess(false);
                json.setMsg("登陆超时，请重新登陆。");
                return json;
            }
            UserAppDto userBean = employeeService.selectComByUserName(loginName);
            json.setObj(userBean);
            json.setMsg("获取成功。");
        } catch (Exception e) {
            log.error("获取当前登陆人绑定房屋信息失败：", e);
            json.setMsg("获取失败。");
            json.setSuccess(false);
        }
        return json;
    }


    /**
     * 根据企业ID获取企业信息
     *
     * @return
     */
    @GetMapping("/getCommunityInfoById")
    @ResponseBody
    public SysDept getCommunityInfoById(String communityId) {
        return deptService.selectById(communityId);
    }

    /**
     * 设置APP默认小区
     * 先把该手机号的人事档案全设置为不默认
     * 设置当前企业人事档案为默认
     *
     * @return
     */
    @PostMapping("/changeCompany")
    @ResponseBody
    public AjaxJson changeCompany(Integer companyId, String username) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(true);
        try {
            employeeService.updateDefaultCompany(companyId, username);
        } catch (Exception e) {
            log.error("设置默认企业失败：", e);
            json.setSuccess(false);
            json.setMsg("设置失败，"+e.getMessage());
        }
        return json;
    }
    /**
     * 是否可以采集指纹
     *
     * @return
     */
    @PostMapping("/collectFinger")
    @ResponseBody
    public AjaxJson collectFinger(Integer companyId) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(true);
        try {
            if(!employeeService.judgeFinger(companyId,getUserName())){
                json.setSuccess(false);
                json.setMsg("指纹采集已过期，若要录入指纹请联系企业管理员");
            };
        } catch (Exception e) {
            log.error("获取指纹采集二维码失败：", e);
            json.setSuccess(false);
            json.setMsg("获取指纹采集二维码失败，"+e.getMessage());
        }
        return json;
    }
}
